package org.example.design.state.test1;

import org.apache.poi.ss.formula.functions.T;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author huangshen
 * @version 0
 * @description 界面
 * @since 2022/2/14 10:06
 */
public class SafeFrame extends Frame implements Context, ActionListener {

    /**
     * 显示当前时间
     */
    private TextField textClock = new TextField(60);
    /**
     * 显示警报中心的记录
     */
    private TextArea textScreen = new TextArea(10,60);
    /**
     * 使用金库按钮
     */
    private Button buttonUse = new Button("使用金库");
    /**
     * 按下警铃按钮
     */
    private Button buttonAlarm = new Button("按下警铃");
    /**
     * 正常通话按钮
     */
    private Button buttonPhone = new Button("正常通话");
    /**
     * 结束按钮
     */
    private Button buttonExit = new Button("结束");
    /**
     * 当前的状态
     */
    private State state = DayState.getInstance();

    public SafeFrame (String title) {
        super(title);
        setBackground(Color.lightGray);
        setLayout(new BorderLayout());

        add(textClock, BorderLayout.NORTH);
        textClock.setEnabled(false);

        add(textScreen, BorderLayout.CENTER);
        textScreen.setEnabled(false);

        Panel panel = new Panel();
        panel.setSize(2400,300);
        buttonUse.setSize(200,100);
        buttonAlarm.setSize(200,100);
        buttonPhone.setSize(200,100);
        buttonExit.setSize(200,100);
        panel.add(buttonUse);
        panel.add(buttonAlarm);
        panel.add(buttonPhone);
        panel.add(buttonExit);
        add(panel, BorderLayout.SOUTH);

        pack();
        setVisible(true);

        buttonUse.addActionListener(this);
        buttonAlarm.addActionListener(this);
        buttonPhone.addActionListener(this);
        buttonExit.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.toString());
        if (e.getSource() == buttonUse) {
            state.doUser(this);
        }
        if (e.getSource() == buttonAlarm) {
            state.doAlarm(this);
        }
        if (e.getSource() == buttonPhone) {
            state.doPhone(this);
        }
        if (e.getSource() == buttonExit) {
            System.exit(0);
        }
    }

    /**
     * 设置时间
     *
     * @param hour 时间
     */
    @Override
    public void setClock(int hour) {
        String clockString = "现在的时间是";
        if (hour < 10) {
            clockString += "0" + hour +  ":00";
        } else {
            clockString += hour + ":00";
        }
        System.out.println(clockString);
        textClock.setText(clockString);
        state.doClock(this, hour);
    }

    /**
     * 改变状态
     *
     * @param state 状态
     */
    @Override
    public void changeState(State state) {
        System.out.println("从" + this.state + "状态变为了" + state + "状态。");
        this.state = state;
    }

    /**
     * 联系警报中心
     *
     * @param msg 消息
     */
    @Override
    public void callSecurityCenter(String msg) {
        textScreen.append("call!" + msg + "\n");
    }

    /**
     * 在警报中心留下记录
     *
     * @param msg 记录
     */
    @Override
    public void reCordLog(String msg) {
        textScreen.append("record... " + msg + "\n");
    }
}
